package com.lb.shopguide.ui.fragment.guide.order;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.index.AdapterIndexDelivery;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.entity.ExpressBean;
import com.lb.shopguide.event.guide.SelectExpressEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import me.yokeyword.indexablerv.IndexableAdapter;
import me.yokeyword.indexablerv.IndexableLayout;

/**
 * Created by kang on 2017/6/5.
 */

public class FragmentSelectDelivery extends BaseCommonFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.indexableLayout)
    IndexableLayout indexableLayout;

    private AdapterIndexDelivery mAdapterIndexDelivery;
    private List<ExpressBean> mChooseDeliveryList;




    public static FragmentSelectDelivery newInstance() {
        return new FragmentSelectDelivery();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_select_delivery;
    }

    @Override
    public void initData() {
        mChooseDeliveryList = new ArrayList<>();
        sendRequestGetExpressList();
    }

    @Override
    public void initView() {

        ntb.setTitleText("选择快递");
        ntb.setLeftImageSrc(R.drawable.iv_back);
        ntb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        indexableLayout.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapterIndexDelivery = new AdapterIndexDelivery(mContext);
        indexableLayout.setAdapter(mAdapterIndexDelivery);
        mAdapterIndexDelivery.setDatas(mChooseDeliveryList);
        indexableLayout.setCompareMode(IndexableLayout.MODE_ALL_LETTERS);

        mAdapterIndexDelivery.setOnItemContentClickListener(new IndexableAdapter.OnItemContentClickListener<ExpressBean>() {
            @Override
            public void onItemClick(View v, int originalPosition, int currentPosition, ExpressBean entity) {
                pop();
                EventBus.getDefault().post(new SelectExpressEvent(entity));
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) getActivity()).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentSelectDelivery.class.getName();
    }


    private void sendRequestGetExpressList() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                JSONObject jsonExpress = JsonUtil.getJsonObjectFromMap(response.getReturnContent());
                try {
                    Iterator it = jsonExpress.keys();
                    while (it.hasNext()) {
                        String key = (String) it.next();
                        JSONArray jsonArray = jsonExpress.getJSONArray(key);
                        if (jsonArray.length() != 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ExpressBean bean = JsonUtil.getGson().fromJson(jsonArray.getString(i), ExpressBean.class);
                                mChooseDeliveryList.add(bean);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mAdapterIndexDelivery.setDatas(mChooseDeliveryList);
                mAdapterIndexDelivery.notifyDataSetChanged();
            }
        };
        ApiMethodGuide.getExpressList(new SimpleLbObserver(listener,otherListener),this, FragmentEvent.DESTROY_VIEW);
    }
}
